const express = require('express')
const detailInfo = express.Router()
const db = require('../../mysql/db')

function query(id) {
    return new Promise((resolve, reject) => {
        db.query(`SELECT * FROM details WHERE id = ${id};`, (err, results) => {
            if (err) {
                reject(err);
            } else {
                if (results.length){
                    let shopIMG = JSON.parse(results[0].shopIMG);
                    let detail = JSON.parse(results[0].detail);
                    let detailIMG = JSON.parse(results[0].detailIMG);
                    let comments = JSON.parse(results[0].comments);
                    let obj = { ...results[0] };
                    obj.shopIMG = shopIMG;
                    obj.detail = detail;
                    obj.detailIMG = detailIMG;
                    obj.comments = comments;
                    resolve(obj);
                }
            }
        });
    });
}
detailInfo.post('/detailInfo', async (req, res) => {
    try {
        const result = await query(req.body.id);
        res.send({
            code: 200, // 0 表示处理成功，1 表示处理失败
            message: 'post 请求成功！', // 状态的描述
            result: result, // 需要响应给客户端的数据
        });
    } catch (err) {
        res.send({
            code: 1, // 0 表示处理成功，1 表示处理失败
            message: err.message, // 状态的描述
            result: null, // 需要响应给客户端的数据
        });
    }
});
module.exports = detailInfo
